﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Xml;
public class CustomPrieteniDetailes
{
    public string idUser;
    public string UserName; 
    public string idPrieten;
    public string PrietenName;
}
public partial class ControlPanel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //pick whatever filename with .xml extension
            string filename = "D:\\UB\\A03S01\\DAW\\SocialBook\\Book.xml";

            XmlDocument xmlDoc = new XmlDocument();
            bool fileIsNew = false;
            try
            {
                xmlDoc.Load(filename);
            }
            catch (System.IO.FileNotFoundException)
            {
                //if file is not found, create a new xml file
                XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                xmlWriter.WriteStartElement("graphml");

                //graphml.SetAttribute("xsi:schemaLocation", "http://graphml.graphdrawing.org/xmlnshttp://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd");
                //If WriteProcessingInstruction is used as above,
                //Do not use WriteEndElement() here
                //xmlWriter.WriteEndElement();
                //it will cause the &ltRoot></Root> to be &ltRoot />
                xmlWriter.Close();
                xmlDoc.Load(filename);
                fileIsNew = true;
            }
            XmlNode root = xmlDoc.DocumentElement;
            XmlElement graphml = xmlDoc.DocumentElement;
            graphml.SetAttribute("xmlns", "http://graphml.graphdrawing.org/xmlns");
            graphml.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            if (!fileIsNew) { graphml.RemoveChild(graphml.FirstChild); }
            XmlElement graph = xmlDoc.CreateElement("graph");
            graph.SetAttribute("id", "G");
            graph.SetAttribute("edgedefault", "undirected");
            root.AppendChild(graph);

            DataContext db = new DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True;");
            Table<aspnet_User> aspnet_User = db.GetTable<aspnet_User>();
            IQueryable<CustomUserDetailes> userQuery = from p in aspnet_User
                                                       select new CustomUserDetailes { id = p.UserId, UserName = p.UserName };
            //"Select * from profile where id = '" + id + "'";
            XmlElement node;
            foreach (CustomUserDetailes i in userQuery)
            {
                node = xmlDoc.CreateElement("node");
                graph.AppendChild(node);
                node.SetAttribute("id", i.UserName);
            }

            Table<Prieteni> Tprieteni = db.GetTable<Prieteni>();
            IQueryable<CustomPrieteniDetailes> prieteniQuery = from p in Tprieteni
                                                               join a in aspnet_User on p.IdUser equals a.UserId
                                                               join b in aspnet_User on p.IdPrieten equals b.UserId
                                                               select new CustomPrieteniDetailes { UserName = a.UserName, PrietenName = b.UserName };
            XmlElement edge;
            foreach (CustomPrieteniDetailes i in prieteniQuery)
            {
                edge = xmlDoc.CreateElement("edge");
                edge.SetAttribute("source", i.UserName);
                edge.SetAttribute("target", i.PrietenName);
                graph.AppendChild(edge);
            }

            xmlDoc.Save(filename);
        }
        catch (Exception ex)
        {
            WriteError(ex.ToString());
        }
    }

    void WriteError(string str)
    {
        LInfo.Text = str;
    }
}